package hflyeer.service.impl;

import hflyeer.dao.UserMapper;
import hflyeer.pojo.dto.UserDto;
import hflyeer.pojo.entity.User;
import hflyeer.service.UserService;
import hflyeer.utils.AssertUtil;
import hflyeer.utils.Md5Util;
import hflyeer.utils.UserIDBase64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * UserServiceImpl
 *
 * @author hflyeer
 * @date 2021-06-03
 * @describe
 */
@Service("UserService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userDao;

    @Override
    public UserDto login(final String userName,final String password) {
        // 判断空值
        AssertUtil.isTrue(StringUtils.isBlank(userName),"用户名不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(password),"密码不能为空！");

        // 判断用户名和密码是否匹配
        User selectUser = userDao.selectByUserName(userName);
        AssertUtil.isTrue(selectUser==null,"用户名不存在");
        AssertUtil.isTrue(!userNameMatchPassword(selectUser,password),"用户名或密码错误");

        UserDto userDto = getUserDto(selectUser);

        return userDto;
    }


    @Override
    public UserDto selectById(Integer userIdFromCookie) {
        User user = userDao.selectByPrimaryKey(userIdFromCookie);
        AssertUtil.isTrue(user==null,"传值错误");
        return getUserDto(user);
    }

    @Override
    public boolean updatePwdById(Integer userId, String oldPwd, String newPwd, String renewPwd) {

        AssertRule(userId, oldPwd, newPwd, renewPwd);

        User selectUser = userDao.selectByPrimaryKey(userId);
        AssertUtil.isTrue(!StringUtils.equals(selectUser.getUserPwd(),Md5Util.encode(oldPwd)),"旧密码输入错误");

        User updateUser = new User();
        updateUser.setId(userId);
        updateUser.setUserPwd(Md5Util.encode(newPwd));
        int update = userDao.updateByPrimaryKeySelective(updateUser);
        if (update>0){
            return true;
        }

        return false;
    }

    @Override
    public List<User> queryAllSales() {
        return userDao.selectAllOfIdAndName();
    }

    // 判断规则
    private void AssertRule(Integer userId, String oldPwd, String newPwd, String renewPwd) {
        AssertUtil.isTrue(Objects.isNull(userId),"用户没有登入");
        AssertUtil.isTrue(StringUtils.isBlank(oldPwd),"请输入旧密码");
        AssertUtil.isTrue(StringUtils.isBlank(newPwd),"请输入新密码");
        AssertUtil.isTrue(StringUtils.isBlank(renewPwd),"请输入确认密码");
        AssertUtil.isTrue(!StringUtils.equals(newPwd,renewPwd),"新密码和确认密码不一致");
        AssertUtil.isTrue(StringUtils.equals(oldPwd,newPwd),"旧密码和新密码一致");
    }

    // 判断用户名和密码是否匹配
    private Boolean userNameMatchPassword(User selectUser, String password) {
        System.out.println(StringUtils.equals(selectUser.getUserPwd(), Md5Util.encode(password))+"======"+selectUser.getUserPwd()+"---"+password);
        return StringUtils.equals(selectUser.getUserPwd(), Md5Util.encode(password));
    }


    private UserDto getUserDto(User selectUser) {
        UserDto userDto = new UserDto();
        userDto.setUserIdStr(UserIDBase64.encoderUserID(selectUser.getId()));
        userDto.setUserName(selectUser.getUserName());
        userDto.setTrueName(selectUser.getTrueName());
        return userDto;
    }
}
